﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.Reflection;

namespace CapsBeep
{
    public partial class Form1 : Form
    {
        private bool _exitClicked = false;

        bool _startWithWindows = false;

        public Form1(bool hideOptions)
        {
            InitializeComponent();

            KeyboardWatcher.KeyboardEvent += _keyboardWatcher_KeyboardEvent;

            KeyboardWatcher.WatchCapsLock = CapsBeep.Properties.Settings.Default.BeepCapsLock;
            KeyboardWatcher.WatchNumLock = CapsBeep.Properties.Settings.Default.BeepNumLock;
            _startWithWindows = GetStartWithWindows();

            checkBoxCapsBeep.Checked = KeyboardWatcher.WatchCapsLock;
            checkBoxNumBeep.Checked = KeyboardWatcher.WatchNumLock;
            checkBoxStartCapsBeep.Checked = _startWithWindows;

            if (hideOptions)
                HideOptions();
        }


        int _lastBalloonTick = 0;
        int _lastBeepTick = 0;
        void _keyboardWatcher_KeyboardEvent(object sender, KeyboardWatcher.KeyboardEventArgs e)
        {
            if (e.IsNotify)
            {
                int lastBeepDelta = Environment.TickCount - _lastBeepTick;
                if (lastBeepDelta > 500)
                {
                    //BackgroundBeep.Beep();
                    System.Media.SystemSounds.Asterisk.Play();

                    _lastBeepTick = Environment.TickCount;
                }

                int lastBalloonDelta = Environment.TickCount - _lastBalloonTick;
                if (lastBalloonDelta > (10000))
                {
                    string message = "";
                    if (e.NotifyCapsLock)
                        message += "CAPS LOCK is ON\r\n";

                    if (e.NotifyNumLock)
                        message += "NUM LOCK is OFF\r\n";

                    notifyIcon1.BalloonTipText = message;
                    notifyIcon1.ShowBalloonTip(1000); // 1 second, the fade in/out takes some time as well.
                    
                    _lastBalloonTick = Environment.TickCount;
                }
            }
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowOptions();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            HideOptions();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _exitClicked = true;
            this.Close();
        }

        private void checkBoxCapsBeep_CheckedChanged(object sender, EventArgs e)
        {
            SetWatchCapsLock(checkBoxCapsBeep.Checked);
        }

        private void checkBoxNumBeep_CheckedChanged(object sender, EventArgs e)
        {
            SetWatchNumLock(checkBoxNumBeep.Checked);
        }

        private void checkBoxStartCapsBeep_CheckedChanged(object sender, EventArgs e)
        {
            if (_startWithWindows != checkBoxStartCapsBeep.Checked)
                SetStartWithWindows(checkBoxStartCapsBeep.Checked);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && !_exitClicked)
            {
                e.Cancel = true;
                HideOptions();
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            ShowOptions();
        }

        private void ShowOptions()
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            
            this.Activate();
        }

        private void HideOptions()
        {
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
        }

        private void linkLabelAbout_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://blueanvilsoftware.com.au/");
        }

        
        const string runRegKeyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

        private string GetStartString()
        {
            Assembly assem = Assembly.GetExecutingAssembly();
            return assem.Location + " -h";
        }

        private bool GetStartWithWindows()
        {
            RegistryKey runRegKey = Registry.CurrentUser.OpenSubKey(runRegKeyName, true);
            if (runRegKey == null)
                return false;

            string startString = runRegKey.GetValue("CapsBeep") as string;
            if (startString == null)
            {
                if (CapsBeep.Properties.Settings.Default.HaveSetStartWithWindows == false)
                {
                    // Default to true!
                    startString = GetStartString();
                    runRegKey.SetValue("CapsBeep", startString);
                    CapsBeep.Properties.Settings.Default.HaveSetStartWithWindows = true;
                    CapsBeep.Properties.Settings.Default.Save();
                    return true;
                }

                return false;
            }
            else
            {
                //Reset the value, just in case current location has changed
                runRegKey.SetValue("CapsBeep", startString);
                return true;
            }
        }

        private void SetStartWithWindows(bool startWithWindows)
        {
            _startWithWindows = startWithWindows;
            
            RegistryKey runRegKey = Registry.CurrentUser.OpenSubKey(runRegKeyName, true);
            if (runRegKey == null)
                return;

            if (startWithWindows)
                runRegKey.SetValue("CapsBeep", GetStartString());
            else if (runRegKey.GetValue("CapsBeep") != null)
                runRegKey.DeleteValue("CapsBeep");
        }

        private void SetWatchCapsLock(bool watch)
        {
            KeyboardWatcher.WatchCapsLock = watch;
            CapsBeep.Properties.Settings.Default.BeepCapsLock = watch;
            CapsBeep.Properties.Settings.Default.Save();
        }

        private void SetWatchNumLock(bool watch)
        {
            KeyboardWatcher.WatchNumLock = watch;
            CapsBeep.Properties.Settings.Default.BeepNumLock = watch;
            CapsBeep.Properties.Settings.Default.Save();
        }



    }
}
